package com.zh.springcloud.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @ClassName TopicRabbitConfig
 * @date: 2021.08.23 09:52
 * @Author: zhanghang
 */
@Configuration
public class TopicRabbitConfig {
	// 正常的topic 队列
	public static final String TEST_TOPIC_EXCHANGE_NAME = "test_topic_exchange_name";
	public static final String TEST_TOPIC_QUEUE_NAME = "test_topic_queue_name";
	public static final String TEST_TOPIC_ROUTING_NAME = "test_topic_routing_name";

	// topic 死信队列
	public static final String TEST_DEAD_TOPIC_EXCHANGE_NAME = "test_dead_topic_exchange_name";
	public static final String TEST_DEAD_TOPIC_QUEUE_NAME = "test_dead_topic_queue_name";
	public static final String TEST_DEAD_TOPIC_ROUTING_NAME = "test_dead_topic_routing_name";

	// 正常的队列
	@Bean
	public Queue createTopicQueue(){
		Map<String, Object> arguments = new HashMap<>(2);
		// 设置过期时间,对 队列设置统一的过期时间，单位为 毫秒
		arguments.put("x-message-ttl",30000);

		// 绑定死信交换机
		arguments.put("x-dead-letter-exchange", TEST_DEAD_TOPIC_EXCHANGE_NAME);
		// 绑定死信的路由key
		arguments.put("x-dead-letter-routing-key", TEST_DEAD_TOPIC_ROUTING_NAME);
		// 绑定死信队列的交换机和路由
		return new Queue(TEST_TOPIC_QUEUE_NAME,true,false,false,arguments);
	}

	@Bean
	public TopicExchange createTopicExchange(){
		// 参数说明：
		// name:    交换机名称
		// durable： 是否持久化
		// autoDelete： 是否自动删除
		return new TopicExchange(TEST_TOPIC_EXCHANGE_NAME,true,false);
	}

	@Bean
	public Binding createTopicBinding(){
		return BindingBuilder.bind(createTopicQueue()).to(createTopicExchange()).with(TEST_TOPIC_ROUTING_NAME);
	}

	// 死信队列
	@Bean
	public Queue createDeadTopicQueue(){
		return new Queue(TEST_DEAD_TOPIC_QUEUE_NAME,true,false,false);
	}

	@Bean
	public TopicExchange createDeadTopicExchange(){
		// 参数说明：
		// name:    交换机名称
		// durable： 是否持久化
		// autoDelete： 是否自动删除
		return new TopicExchange(TEST_DEAD_TOPIC_EXCHANGE_NAME,true,false);
	}

	@Bean
	public Binding createDeadTopicBinding(){
		return BindingBuilder.bind(createDeadTopicQueue()).to(createDeadTopicExchange()).with(TEST_DEAD_TOPIC_ROUTING_NAME);
	}

}
